using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using TMPro;
using System;
using Framework;

public class GameUI : MonoBehaviour
{
    public TMP_InputField CmdInput;
    public TMP_InputField PlayerInput;
    public Button EnterBtn;
    public Button ConfirmBtn;
    public TextMeshProUGUI TimeTmp;

    private Sprite[] _sprites;
    private char tmpName = 'A';

    private Dictionary<string, Player> _players = new Dictionary<string, Player>();

    // Start is called before the first frame update
    void Start()
    {
        _sprites = ResourcesMgr.LoadAllSprite("Icon");

        EnterBtn.onClick.AddListener(OnEnterBtnClick);
        ConfirmBtn.onClick.AddListener(OnConfirmBtnClick);
    }

    private void OnConfirmBtnClick()
    {
        string name = PlayerInput.text;
        string cmd = CmdInput.text;
        switch (cmd)
        {
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
                int.TryParse(cmd, out int value);
                EventCenter.Send(EventType.PLAYER_SWITCH_MAP, new object[] { name, FoodMgr.Ins.GetMap(value - 1).transform });
                break;
            default:
                break;
        }
    }

    private void OnEnterBtnClick()
    {
        Transform parent = FoodMgr.Ins.GetMap().transform;
        GameObject go = Instantiate(ResourcesMgr.Load<GameObject>("PlayerItem1"), transform, false);
        if (go.TryGetComponent(out Image img))
        {
            img.sprite = _sprites[UnityEngine.Random.Range(0, _sprites.Length)];
        }
        if (go.TryGetComponent(out Player player))
        {
            string name = tmpName.ToString();
            player.Init(parent, name);
            tmpName++;
            if (tmpName > 90)
            {

            }
            _players[name] = player;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void UpdateTime(string value)
    {
        TimeTmp.text = value;
    }
}
